package com.sc.services;

import com.sc.mapper.UserMapper;
import com.sc.pojo.BookField;
import com.sc.pojo.Comment;
import com.sc.pojo.User;

import com.sc.utils.DataSourceConfig;
import com.sc.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ConcurrentModificationException;
import java.util.List;

/**
 * @description: 用户services层
 * @author: sc
 * @create: 2023-01-04 16:51
 **/
public class UserServices {
    //创建工厂
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DataSourceConfig.class);
    PooledDataSource d = ctx.getBean("dataSource",PooledDataSource.class);
    Connection con;

    {
        try {
            con = d.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户是否存在
     * @param userid
     * @return
     */
    public boolean isUserExist(String userid){
        SqlSession sqlSession = factory.openSession(con);

        System.out.println(sqlSession.getConnection());
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectById(userid);
        boolean flag=true;
        if(user == null){
            flag = false;
        }
        sqlSession.close();
        return flag;
    }



    /**
     * 获得用户对象
     * @param userid
     * @return
     */
    public User getUser(String userid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectById(userid);
        sqlSession.close();
       return user;
    }


    /**
     *
     * @param input
     * @param password
     * @return
     */
    public boolean isRightPassword(String input,String password){
        boolean flag=true;
        if(!input.equals(password)){
            flag = false;
        }
        return flag;

    }

    /**
     * 添加用户场地预定信息
     * @param bookFieldMes
     */

    public void addBookMes(BookField bookFieldMes){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addBookFieldMes(bookFieldMes);
        sqlSession.commit();
        sqlSession.close();

    }

    /**
     * 查找是否有重复预定信息
     * @param bookField
     * @return
     */
    public boolean selectBy(BookField bookField){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        BookField bookField1 = mapper.selectBy(bookField);
        boolean flag=false;
        if(bookField1!=null){
            flag = true;
        }
        sqlSession.close();
        return flag;
    }

    /**
     * 根据用户ID查找所有预定场地信息
     * @param userid
     * @return
     */
    public List<BookField> selectByUserId(String userid){

        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<BookField> bookFields = mapper.selectAllMyBookMes(userid);
        sqlSession.close();
        return bookFields;
    }


    /**
     * 查找所有预定场地信息
     * @param
     * @return
     */
    public List<BookField> selectAllBookMes(){

        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<BookField> bookFields = mapper.selectAllBookMes();
        sqlSession.close();
        return bookFields;
    }

    /**
     * 根据预定时间查找所有预定场地信息
     * @param
     * @return
     */
    public List<BookField> selectBookMesByDatTime(String bookday){

        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<BookField> bookFields = mapper.selectByDayTime(bookday);
        sqlSession.close();
        return bookFields;
    }

    /**
     * 根据场地号查询该场地所有评论
     * @param fieldid
     * @return
     */
    public List<Comment> selectCommentByFieldId(int fieldid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Comment> comments = mapper.selectAllComment(fieldid);

        sqlSession.close();
        return comments;

    }


    /**
     * 查询所有评论
     * @param
     * @return
     */
    public List<Comment> selectAllComment(){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Comment> comments = mapper.adminSelectAllComment();

        sqlSession.close();
        return comments;

    }

    /***
     * 添加用户对场地的评论信息
     * @param comment
     */
    public void addComment(Comment comment){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addComment(comment);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据userid查询该场地所有评论
     * @param userid
     * @return
     */
    public List<Comment> selectCommentByUserId(String userid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Comment> comments = mapper.selectAllCommentByUserId(userid);

        sqlSession.close();
        return comments;

    }

    /**
     * 根据场地号用户ID和时间 删除评论
     * @param comment
     */
    public void deleteComment(Comment comment){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteComment(comment);
        sqlSession.commit();
        sqlSession.close();

    }

    /**
     * 根据预约号修改预约状态
     * @param fieldbookid
     */
    public void updateBookState(String fieldbookid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateFieldBookState(fieldbookid);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据预约号修改预约状态
     * @param fieldbookid
     */
    public void refuseBookState(String fieldbookid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.refuseFieldBookState(fieldbookid);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据预约号删除预约信息
     * @param fieldbookid
     */
    public void deleteFieldBookMes(String fieldbookid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteFieldBookMes(fieldbookid);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 管理员查询所有用户信息
     * @return
     */
    public List<User> selectAllUser(){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectAllUser();
        return users;

    }

    /**
     * 注册用户信息时，判断更新后的id是否重复
     * @param userid
     * @return
     */
    public boolean isUpdateUserExist(String userid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        boolean flag=false;
        User user = mapper.selectById(userid);
        if(user!=null)
            flag = true;
        sqlSession.close();
        return flag;
    }

    /**
     * 管理员更新用户信息
     * @param user
     */
    public void updateUserMes(User user){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUserMes(user);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 管理员删除用户
     * @param userid
     */
    public void deleteUserByUserId(String userid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUserByUserId(userid);
        sqlSession.commit();
        sqlSession.close();
    }

    public String selectAdminName(String adminid){
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        String s = mapper.selectAdminName(adminid);
        sqlSession.close();
        return s;
    }

    /**
     * 判断用户提交预约时间是否重合
     * @param bookField
     * @return
     */
    public boolean isConflict(BookField bookField){
        boolean flag=false;

        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<BookField> conflict = mapper.isConflict(bookField);
        List<BookField> conflict2 = mapper.isConflict2(bookField);
        if(conflict.size()!=0||conflict2.size()!=0){
            flag=true;
        }
        sqlSession.close();
        return flag;
    }

    /**
     * 添加用户信息
     * @param user
     */

    public void addUser(User user) {
        SqlSession sqlSession = factory.openSession(con);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
}